﻿Public Class DrangoEdad

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of ErangoEdad)
        Dim rangoEdades As List(Of ErangoEdad) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_rangoEdad_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            rangoEdades = New List(Of ErangoEdad)
                            Dim rangoEdad As ErangoEdad = Nothing
                            Do While dr.Read
                                rangoEdad = New ErangoEdad( _
                                    dr.GetInt32(dr.GetOrdinal("idRangoEdad")), _
                                    dr.GetString(dr.GetOrdinal("rangoEdadDe")), _
                                    dr.GetString(dr.GetOrdinal("rangoEdadHasta")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                rangoEdades.Add(rangoEdad)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return rangoEdades
    End Function
    Public Shared Function SeleccionarXEstado(ByVal descEmpresa As String) As List(Of ErangoEdad)
        Dim rangoEdades As List(Of ErangoEdad) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_rangoEdad_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            rangoEdades = New List(Of ErangoEdad)
                            Dim rangoEdad As ErangoEdad = Nothing
                            Do While dr.Read
                                rangoEdad = New ErangoEdad( _
                                    dr.GetInt32(dr.GetOrdinal("idRangoEdad")), _
                                    dr.GetString(dr.GetOrdinal("rangoEdadDe")), _
                                    dr.GetString(dr.GetOrdinal("rangoEdadHasta")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                rangoEdades.Add(rangoEdad)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return rangoEdades
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pRangoEdad As ErangoEdad) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_rangoEdad_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@rangoEdadDe", SqlDbType.VarChar, 55).Value = pRangoEdad.rangoEdadDe
                cmd.Parameters.Add("@rangoEdadHasta", SqlDbType.VarChar, 55).Value = pRangoEdad.rangoEdadHasta
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pRangoEdad.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal pRangoEdad As ErangoEdad) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_rangoEdad_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idRangoEdad", SqlDbType.Int).Value = pRangoEdad.idRangoEdad
                cmd.Parameters.Add("@rangoEdadDe", SqlDbType.VarChar, 55).Value = pRangoEdad.rangoEdadDe
                cmd.Parameters.Add("@rangoEdadHasta", SqlDbType.VarChar, 55).Value = pRangoEdad.rangoEdadHasta
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = pRangoEdad.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pRangoEdad.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal pRangoEdad As ErangoEdad) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_rangoEdad_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idRangoEdad", SqlDbType.Int).Value = pRangoEdad.idRangoEdad
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pRangoEdad.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

    '******* RptGerenciales xRangoEdad  *******'
    Public Shared Function rptGerenciales_xRangoEdad(ByVal descEmpresa As String, ByVal rptGerencial As EproformaCabecera) As List(Of ErangoEdad)
        Dim rangoEdads As List(Of ErangoEdad) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_rptGerencial_xRangoEdad", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idProyecto", SqlDbType.Int).Value = rptGerencial.idProyecto
                cmd.Parameters.Add("@idEtapa", SqlDbType.Int).Value = rptGerencial.idEtapa
                cmd.Parameters.Add("@FechaInicial", SqlDbType.Char, 10).Value = rptGerencial.fchInicio
                cmd.Parameters.Add("@FechaFinal", SqlDbType.Char, 10).Value = rptGerencial.fchFin
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            rangoEdads = New List(Of ErangoEdad)
                            Dim rangoEdad As ErangoEdad = Nothing
                            Do While dr.Read
                                rangoEdad = New ErangoEdad( _
                                    dr.GetInt32(dr.GetOrdinal("idRangoEdad")), _
                                    dr.GetString(dr.GetOrdinal("rangoEdadDe")), _
                                    dr.GetString(dr.GetOrdinal("rangoEdadHasta")), _
                                    dr.GetInt32(dr.GetOrdinal("cantidad")), _
                                    dr.GetDecimal(dr.GetOrdinal("porcentaje")))
                                rangoEdads.Add(rangoEdad)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return rangoEdads
    End Function

End Class
